package cn.by.wms.controller;

import cn.by.wms.dto.PoStatusDto;
import cn.by.wms.enums.PoStatus;
import cn.by.wms.service.PoItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import cn.by.wms.entity.*;
import cn.by.wms.service.PoMasterService;

import java.util.List;

/**
 * ;(206_po_master)控制层
 * @author : heimi
 * @date : 2024-11-25
 */
@RestController
@RequestMapping("/api/po")
public class PoMasterController{
    @Autowired
    private PoMasterService poMasterService;
    @Autowired
    private PoItemService poItemService;
    
    /** 
     * 查询接口
     */
     @GetMapping
     public List<PoMaster> select(PoMasterQuery query){
        return poMasterService.select(query);
    }
    
    @PostMapping
    @PreAuthorize("hasAnyRole('ROLE_PURCHASE','ROLE_PURCHASE_MANAGER')")
    public Integer insert(@RequestBody PoMaster product){

       return poMasterService.insert(product);
    }


    @GetMapping("/approve")
//    @PreAuthorize("hasAnyRole('ROLE_PURCHASE','ROLE_PURCHASE_MANAGER')")
    @PreAuthorize("hasPermission(#poId,'PO_APPROVE')")
    public Integer approve(String poId){
         PoStatusDto poStatusDto = new PoStatusDto().setId(poId).setStatus(PoStatus.APPROVE.getCode());
        return poMasterService.update(poStatusDto);
    }

    @GetMapping("/reject")
    @PreAuthorize("hasPermission('PO.REJECT.BUTTON','PO_REJECT')")
    public Integer reject(String poId){
        PoStatusDto poStatusDto = new PoStatusDto().setId(poId).setStatus(PoStatus.REJECT.getCode());
        return poMasterService.update(poStatusDto);
    }
    @PutMapping 
    public Integer update(@RequestBody PoStatusDto poStatusDto){
        return poMasterService.update(poStatusDto);
    }
    
    @DeleteMapping
    public Integer delete(Integer id){
        return poMasterService.delete(id);
    }

    @GetMapping("/item")
    public List<PoItem> select(PoItemQuery query){
        return poItemService.select(query);
    }
//
//    @PostMapping
//    public Integer insert(@RequestBody PoItem product){
//        return poItemService.insert(product);
//    }
//
//    @PutMapping
//    public Integer update(@RequestBody PoItem product){
//        return poItemService.update(product);
//    }
//
//    @DeleteMapping
//    public Integer delete(Integer id){
//        return poItemService.delete(id);
//    }
}